#include "puzzlewindow.h"
#include <QMessageBox>
#include <QRandomGenerator>

PuzzleWindow::PuzzleWindow(QWidget *parent)
    : QWidget(parent), emptyRow(2), emptyCol(2)
{
    QGridLayout *gridLayout = new QGridLayout(this);
    gridLayout->setSpacing(2);

    // 初始化按钮
    int number = 1;
    for (int row = 0; row < 3; ++row) {
        for (int col = 0; col < 3; ++col) {
            if (row == 2 && col == 2) {
                buttons[row][col] = new QPushButton();
                buttons[row][col]->setFixedSize(60, 60);
            } else {
                buttons[row][col] = new QPushButton(QString::number(number));
                buttons[row][col]->setFixedSize(60, 60);
                number++;
            }
            gridLayout->addWidget(buttons[row][col], row, col);
            connect(buttons[row][col], &QPushButton::clicked, this, &PuzzleWindow::handleButtonClick);
        }
    }

    shuffle(); // 打乱拼图
}

void PuzzleWindow::handleButtonClick()
{
    QPushButton* clickedButton = qobject_cast<QPushButton*>(sender());

    // 找到点击按钮的位置
    for (int row = 0; row < 3; ++row) {
        for (int col = 0; col < 3; ++col) {
            if (buttons[row][col] == clickedButton) {
                // 检查是否与空位相邻
                if ((qAbs(row - emptyRow) == 1 && col == emptyCol) ||
                    (qAbs(col - emptyCol) == 1 && row == emptyRow)) {

                    swapButtons(row, col, emptyRow, emptyCol);
                    emptyRow = row;
                    emptyCol = col;

                    if (checkPuzzleSolved()) {
                        QMessageBox::information(this, "成功", "恭喜，拼图完成！");
                    }
                }
                return;
            }
        }
    }
}

void PuzzleWindow::swapButtons(int row1, int col1, int row2, int col2)
{
    QString temp = buttons[row1][col1]->text();
    buttons[row1][col1]->setText(buttons[row2][col2]->text());
    buttons[row2][col2]->setText(temp);
}

void PuzzleWindow::shuffle()
{
    // 进行50次随机有效移动
    for (int i = 0; i < 50; ++i) {
        QVector<QPair<int, int>> possibleMoves;

        // 寻找所有可能的移动方向
        if (emptyRow > 0) possibleMoves.append(qMakePair(emptyRow - 1, emptyCol));
        if (emptyRow < 2) possibleMoves.append(qMakePair(emptyRow + 1, emptyCol));
        if (emptyCol > 0) possibleMoves.append(qMakePair(emptyRow, emptyCol - 1));
        if (emptyCol < 2) possibleMoves.append(qMakePair(emptyRow, emptyCol + 1));

        // 随机选择一个有效移动
        if (!possibleMoves.isEmpty()) {
            int index = QRandomGenerator::global()->bounded(possibleMoves.size());
            QPair<int, int> move = possibleMoves[index];
            swapButtons(move.first, move.second, emptyRow, emptyCol);
            emptyRow = move.first;
            emptyCol = move.second;
        }
    }
}

bool PuzzleWindow::checkPuzzleSolved()
{
    int expected = 1;
    for (int row = 0; row < 3; ++row) {
        for (int col = 0; col < 3; ++col) {
            if (row == 2 && col == 2) {
                if (!buttons[row][col]->text().isEmpty()) return false;
            } else {
                if (buttons[row][col]->text() != QString::number(expected)) return false;
                expected++;
            }
        }
    }
    return true;
}
